
import java.util.List;

/**
 * @Author 12629
 * @Description：
 */
public class MySingleList {

    /**
     * 节点内部类
     */
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//不初始化了 默认就是null

    public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    /**
     * 打印链表里面的数据
     */
    public void display() {
        ListNode cur = this.head;
        while (cur.next != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
        }else{
            ListNode cur = this.head;
            while(cur.next != null){
            cur = cur.next;
            }
            cur.next = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(index < 0 || index > size()){
            System.out.println("下标不合法");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if (index > size()){
            addLast(data);
            return;
        }
        while (index -1!= 0){
            cur = cur.next;
            index --;
        }
        node.next = cur.next;
        cur.next = node;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //空链表
        if(this.head == null){
            return;
        }
        //删头节点为key
        if(this.head.val == key){
            head = head.next;
        }
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                //删key
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }
        if(cur.next == null) {
            System.out.println("没有你要找的数字");
            return;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head ==null) {
            return;
        }
        ListNode prev= this.head;
        ListNode cur = prev.next;
        while(cur!=null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }//删除头节点
        if(this.head.val == key){
           this.head = this.head.next;
        }
    }


    public void clear() {
        this.head = null;
    }
    //给定一个头结点为 head 的非空单链表，返回链表的中间结点。
    //如果有两个中间结点，则返回第二个中间结点。
    public ListNode middleNode() {
        //快慢指针，相同的路程，快的是慢的速度的二倍，
        // 相同时间内，快的到了终点，慢的刚好到中间
        ListNode fast = this.head;//走两步
        ListNode slow = this.head;//走一步
        if(fast.next==null){
            return null;
        }//前后不能换位置，否者会空指针异常
        //     节点为偶数个    奇数个
        while(fast!=null &&fast.next!=null ){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public boolean chkPalindrome(ListNode head) {
// 判断链表是否为回文字符串
        ListNode fast = head;
        ListNode slow = head;
        //1.慢指针找到中间位置
        while (fast!=null&&fast.next!=null){
            fast =fast.next.next;
            slow = slow.next;
        }//翻转后半段链表
       ListNode cur = slow.next;
        while(cur!=null){
            ListNode curnext = cur.next;
            cur.next = slow;
            slow = cur;
            cur =curnext;
        }//判断前面的和后面的是否相同，分奇数偶数个链表
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            head = head.next;
            slow = slow.next;
        }
        if(head.next == slow){

        }
        return true;
    }
    public class Solution {
        public boolean hasCycle(ListNode head) {
            //  快慢指针，快的走两步，慢点走一步，若有环当慢的走一圈时一定能相遇
            ListNode fast = head;
            ListNode slow = head;
            //带环的链表next里面没有null，所以遇见null一定是链表遍历完了，即无环
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                //两个指针每动一次就判断一次是否相遇
                if (fast == slow) {
                    return true;
                }
            }
            return false;
        }
    }
}




